﻿@{
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" lay-filter="editForm" action="">
    <div class="layui-form-item">
        <input type="hidden" name="id" />
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">领料单编号</label>
        <div class="layui-input-block">
            <input type="text" name="requisitionCode" lay-verify="required" placeholder="请输入仓库编码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">领料单名称</label>
        <div class="layui-input-block">
            <input type="text" name="requisitionName" lay-verify="required" placeholder="请输入仓库名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">领料单日期</label>
        <div class="layui-input-block">
            @* <input type="text" name="location" placeholder="请输入位置" autocomplete="off" class="layui-input"> *@
            <input type="date" name="requisitionDate" id="requisitionDate" placeholder="请选择日期" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">生产工单</label>
        <div class="layui-input-block">
            @* <input type="number" name="warehouseArea" lay-verify="required|number" placeholder="请输入面积" autocomplete="off" class="layui-input"> *@
            <select name="workOrderCode" id="workOrderCode" lay-filter="workOrderCode">
                <option value="">请选择生产工单</option>
            </select>

        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">工作站</label>
        <div class="layui-input-block">
            @* <input type="text" name="warehouseHead" placeholder="请输入负责人" autocomplete="off" class="layui-input"> *@
            <select name="workStation" id="workStation" lay-filter="workStation">
                <option value="">请选择工作站</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">客户</label>
        <div class="layui-input-block">
            <input type="text" name="customerCode" placeholder="请输入客户名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单据状态</label>
        <div class="layui-input-block">
            <input type="radio" name="dstatus" value=1 title="是" checked />
            <input type="radio" name="dstatus" value=2 title="否" />
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="demo2">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>

<script>
    layui.use(['form'], function(){
           var form = layui.form;
           var $ = layui.$;
           var layer = layui.layer;

           // 加载数据方法
           window.loadData = function(id) {
               $.get('/ProductionRequisition/GetProductionRequisitionById', { id: id }, function(res){
                   console.log('获取到的数据:', res);
                   if(res.code === 0){
                       var formData = {
                           id: res.data.result.id,
                           requisitionCode: res.data.result.requisitionCode,
                           requisitionName: res.data.result.requisitionName,
                           requisitionDate: res.data.result.requisitionDate.toString().substr(0,10),
                           workOrderCode: res.data.result.workOrderCode,
                           workStation: res.data.result.workStation,
                           customerCode: res.data.result.customerCode,
                           dstatus: res.data.result.dstatus,
                                 
                       };
                       form.val('editForm', formData);
                       form.render();
                   } else {
                       layer.msg('获取数据失败');
                   }
               });
           }

           // 提交事件
            form.on('submit(demo2)', function (data) {
              var field = data.field; // 获取表单字段值
              // 显示填写结果，仅作演示用
              // layer.alert(JSON.stringify(field), {
              //   title: '当前填写的字段值',
              // });
              // 此处可执行 Ajax 等操作

                  $.ajax({
                              url:"/ProductionRequisition/UpdateProductionRequisition",
                              data:field,
                              type:"post",
                              dataType:"json",
                              success:function(res){
                                  console.log(res)
                                  if(res>0)
                                  {
                                     alert("修改成功")

                                     // 刷新父页面表格
                                parent.layui.table.reload('warehouseTable');
                                // 关闭弹窗
                                var index = parent.layer.getFrameIndex(window.name);
                               
                              parent.layer.close(index);
                                  }
                                  else
                                  {
                                      alert("修改失败")
                                  }
                              }
                          })
              return false; // 阻止默认 form 跳转
            });


            // 选择生产工单
      var funworderCode = function(){
            $.ajax({
                url: "/ProductionRequisition/GetProductionWorkOrder",
                type: "get",
                dataType: "json",
                success: function (res) {
                    console.log("生产工单:" + res.data)
                    var data = res;
                    var html = "";
                    for (var i = 0; i < data.length; i++) {
                        html += "<option value='" + data[i].workOrderName + "'>" + data[i].workOrderName + "</option>";
                    }
                    $("#workOrderCode").html(html);
                    form.render('select');
                }
            });
       }

       // 选择工作站
       var funworkStation = function(){
            $.ajax({
                url: "/ProductionRequisition/GetWorkstation",
                type: "get",
                dataType: "json",
                success: function (res) {
                    console.log( "工作站:" + res.data)
                    var data = res;
                    var html = "";
                    for (var i = 0; i < data.length; i++) {
                        html += "<option value='" + data[i].workstationName + "'>" + data[i].workstationName + "</option>";
                    }
                    $("#workStation").html(html);
                    form.render('select');
                }
            });
       }

       //workStation
        funworderCode()
        funworkStation()

    });
</script>

